package org.qloudgen.borg.pages;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.time.Duration;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.AppBean;
import org.qloudgen.borg.metadb.DefaultImpl;
import org.qloudgen.borg.pages.common.BasePage;
import org.qloudgen.borg.pages.model.AppModel;

import com.google.common.collect.Lists;
import org.apache.wicket.markup.html.form.FormComponent;

@SuppressWarnings({"unchecked","rawtypes"})
public class AppListPage extends BasePage{

	/**
	 * @Author youngeryoyo
	 * youngeryoyo@gmail.com
	 */

	static Log log = LogFactory.getLog(ClusterListPage.class);

	private static final long serialVersionUID = 1L;
	private transient List<AppBean> apps = Lists.newArrayList();
	private transient AppModel dsm;
	private transient String buttonName;
	
	public AppListPage(PageParameters pageParameters){
		
		super();
		this.pp = pageParameters;
	
		// get userno's apps from metadb
		DefaultImpl.getAppList(apps);
		
		// add form 
		Form<?> form = new Form<Object>("form") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit() {
				
				dsm = (AppModel)getDefaultModelObject();

				buttonName = ((FormComponent<String>)findSubmittingButton()).getInputName();
				
				if (dsm.getGroup() == null) {
					
					info(CommonValues.INFO_NO_SELECT);
					
				} else {
					
					if (buttonName.equals("delete")) DefaultImpl.deleteApp(dsm.getGroup().getAppNo());	
					setResponsePage(AppListPage.class);
					
				}
			
			}
			
		};
		
		form.setDefaultModel(new CompoundPropertyModel<AppModel>(new AppModel()));
		this.add(form);
	
		// add group
		final RadioGroup<?> group=new RadioGroup<Object>("group"); 
		group.setOutputMarkupId(true);
		group.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(CommonValues.LIST_REFRESH_TIME)){	
			
			private static final long serialVersionUID = 1L;
			
			@Override
			protected  void	onPostProcessTarget(AjaxRequestTarget target)  {
				DefaultImpl.getAppList(apps);	
				target.add(group);	
			}
			
		});
		group.setRenderBodyOnly(false);
		form.add(group);
		
		// add cluster list
		final PageableListView<AppBean> listView =new PageableListView<AppBean>( "rows", apps, CommonValues.SHOW_LIST_NUMBER) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<AppBean> item) {
				final AppBean app = (AppBean) item.getModelObject();
				item.add(new Radio<AppBean>("radio", new Model<AppBean>(app)));
				item.add( new Label( "id", app.getAppNo()));
				item.add( new Label( "name",app.getAppName()));
				item.add( new Label( "desc", app.getAppDesc()));
				item.add( new Label( "clusters", app.getClusterStr()));
				
				Label count = new Label( "count", String.valueOf(app.getFuncCount()));
				
				Link link = new Link("funccount"){

					private static final long serialVersionUID = 1L;

					@Override
					public void onClick() {
						setResponsePage(AppFuncListPage.class, new PageParameters().set("appNo", app.getAppNo()).set("appName", app.getAppName()));
						
					}};
				link.add(count);
				item.add(link );
				
				if (item.getIndex() % 2 == 1) {
					item.add(new AttributeModifier("class",new Model<String>("even")));
					} else {
					item.add(new AttributeModifier("class",new Model<String>("odd")));
					}
			}
		};
		listView.setOutputMarkupId(true);
		group.add(listView);
		
		// add navigator
		final AjaxPagingNavigator nav = new AjaxPagingNavigator("navigator", listView);
		nav.setOutputMarkupId(true);
		group.add(nav);
		
		// add button 
		form.add(new Button("delete"));
		
		// add refresh
		form.add(new AjaxLink<Object>("refresh"){
			private static final long serialVersionUID = 1L;
			public void onClick(AjaxRequestTarget target) {
				DefaultImpl.getAppList(apps);
				target.add(group);
			}
		});		
	
	}

}

